// pages/possword/possword.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    o_type:'password',
    o_icon:'icon-icon-test',
    t_type: 'password',
    t_icon: 'icon-icon-test',
    phone:'',
    user_type:1,
    possword:'',
    f:0
  },
  o_swich(){
    var that = this;
    var types = that.data.o_type;
    console.log(types)
    var icon = that.data.o_icon;
    if (types =='password'){
      that.setData({
        o_type:'password',
        o_icon:'icon-xianshikejian'
      })
    }else{
      that.setData({
        o_type: 'text',
        o_icon: 'icon-icon-test'
      })
    }
  },
  t_swich() {
    var that = this;
    var types = that.data.t_type;
    console.log(types)
    var icon = that.data.t_icon;
    if (types == 'password') {
      that.setData({
        t_type: 'text',
        t_icon: 'icon-xianshikejian'
      })
    } else {
      that.setData({
        t_type: 'password',
        t_icon: 'icon-icon-test'
      })
    }
  },
 
  inputPassword(e){
    this.setData({
      possword:e.detail.value
    })
  },
  inputConfirPassword(e){
    this.setData({
      repassword: e.detail.value
    })
  },
  submit(e){
    var that = this;
    var possword = e.detail.value.possword;
    var repassword = e.detail.value.repassword;
    if (!possword) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return false;
    } else if (!repassword) {
      wx.showToast({
        title: '请再输入密码',
        icon: 'none'
      })
      return false;
    }
    else if (possword != repassword) {
      wx.showToast({
        title: '密码不一致',
        icon: 'none'
      })
      return false;
    } else {
      var user_data = e.detail;
      wx.login({
        success(ress) {
          let param = {
            password: that.data.possword,
            phone: that.data.phone
          };
          wx.showLoading({})
          util.commonRequest('modifyPassword', 'post', param, function (res) {
            wx.hideLoading();
            if (res.data.code == 200) {
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 1500
              })
              wx.setStorageSync('userInfo', '');
              setTimeout(function () {				//确认之后跳转的页面
                wx.navigateTo({
                  url: '../login/login?type=' + that.data.user_type,
                })
              }, 1500);
            } else {
              wx.showToast({
                icon: 'loading',
                title: res.data.msg,
              })
            }
          });
        }
      })
    }
  },
  onGotUserInfo(e) {
    var that = this;
    var possword = that.data.possword;
    var repassword = that.data.repassword;
    if (!possword) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return false;
    } else if (!repassword) {
      wx.showToast({
        title: '请再输入密码',
        icon: 'none'
      })
      return false;
    }
    else if (possword != repassword) {
      wx.showToast({
        title: '密码不一致',
        icon: 'none'
      })
      return false;
    } else {
      var user_data = e.detail;
      wx.login({
        success(ress){
          let param = {
            password: that.data.possword,
            phone: that.data.phone,
            code: ress.code,
            rawData: user_data.rawData,
            signature: user_data.signature,
            encryptedData: user_data.encryptedData,
            iv: user_data.iv
          };
          wx.showLoading({})
          util.commonRequest('getUserInfo', 'post', param, function (res) {
            wx.hideLoading();
            if (res.data.code == 200) {
              wx.showToast({
                title: '注册成功',
                icon: 'success',
                duration: 1500
              })
              setTimeout(function () {				//确认之后跳转的页面
                wx.navigateTo({
                  url: '../login/login?type=' + that.data.user_type,
                })
              }, 1500);
            } else {
              wx.showToast({
                icon: 'loading',
                title: res.data.msg,
              })
            }
          });
        }
      })
    }
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var phone = options.phone;
    var type = options.type;
    var f = options.f;
    if(!f){
      this.setData({
        phone: phone,
        user_type: type,
        f: 0
      })
    }else{
      this.setData({
        phone: phone,
        user_type: type,
        f: 1
      })
    }
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})